<template>
  <div class="home">
    <el-container>
      <!-- router属性表示开启路由模式 -->
      <!-- collapse表示控制导航菜单的展开和折叠 -->
      <!-- unique-opened表示是否只保持一个子菜单的展开 -->
    <el-aside width="auto"><el-menu
      router
      unique-opened
      default-active="2"
      background-color="#f9f9f9"
      class="el-menu-admin"
      @open="handleOpen"
      :collapse="isCollapse"
      @close="handleClose">
      <div class="logo"></div>
        <el-submenu :index="menu.path" v-for="menu in menuList" :key="menu.id">
          <template slot="title">
             <i class="el-icon-location"></i>
             <span>{{menu.authName}}</span></template>
             <!--index会将路由的path进行跳转，因为通过router属性开启了路由模式 -->
          <el-menu-item  :index="menusecond.path" v-for="menusecond in menu.children" :key="menusecond.id">
            <span>{{menusecond.authName}}</span>
            </el-menu-item>
        </el-submenu>
    </el-menu></el-aside>
    <el-container>
      <el-header>
        <div @click='collapse'>
            <div class="myicon myicon-menu toggle-btn"></div>
        </div>

        <div class="system-title">电商后台管理系统</div>
        <div>
          <span class="welcome">你好,{{username}}</span>
           <el-button type="warning" @click='Logout'>退出</el-button>
        </div>
      </el-header>
      <el-main>
         <transition
            enter-active-class="animated fadeIn "
            leave-active-class="animated fadeOut"
            mode="out-in"
          >
        <router-view>
        </router-view>
         </transition>
      </el-main>
    </el-container>
  </el-container>
  </div>
</template>

<script>
// :collapse="isCollapse"用于隐藏和显示左侧边栏，配合点击事件使用另值发生修改，false为显示，true为隐藏
import { getUserList, menusList } from '@/api/index.js'
export default {
  // 创建一个名为createdata的函数，里面设置的getUserList是从api的index.js中导入的方法，传入3个参数
  // query(查询参数)，pagenum(当前页码),pagesize（每页显示的条数）
  createdata () {
    getUserList({ 'query': '', 'pagenum': 1, 'pagesize': 1 })
    // 获取返回值
      .then(res => {
        if (res) {
          // console.log(res)
        } else {
          // console.log(res)
        }
      })
      .catch(res => {
        // console.log(res)
      })
  },
  data () {
    return {
      isCollapse: false,
      username: localStorage.getItem('username'),
      menuList: []
    }
  },
  created () {
    this.initmenulist()
  },
  methods: {
    initmenulist () {
      menusList()
        .then(res => {
          console.log(res.data.data)
          this.menuList = res.data.data
        })
    },
    handleOpen (key, keyPath) {
      // console.log(key, keyPath)
    },
    handleClose (key, keyPath) {
      // console.log(key, keyPath)
    },
    // 用于显示隐藏左侧边栏
    collapse () {
      this.isCollapse = !this.isCollapse
    },
    // 退出登录清除token值并跳转到登录页
    Logout () {
      localStorage.clear()
      this.$router.push('/login')
    }
  }
}
</script>

<style lang="scss" scoped>
.home {
  height: 100%;
  .el-menu-admin:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
  .el-container {
    height: 100%;
  }
  .el-aside {
    background-color: #F9F9F9;
  }
  .el-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background-color: #409EFF;
  }
  .logo {
    height:60px;
    background: url(../assets/logo.png);
    background-size: cover;
    background-color: white;
  }
  .toggle-btn {
    padding: 0 15px;
    margin-left: -20px;
    font-size: 36px;
    color: white;
    cursor: pointer;
    line-height: 60px;
    &:hover {
      background-color: #4292CF;
    }
  }
  .system-title {
    font-size: 28px;
    color: white;
  }
  .welcome, {
    color: white;
  }
}
</style>
